package com.preguicoso.client;

import static com.google.gwt.query.client.GQuery.$;
import static com.google.gwt.query.client.plugins.Effects.Effects;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.query.client.Function;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.RootPanel;
import com.preguicoso.client.busca.Busca;
import com.preguicoso.client.busca.ListaEstabelecimento;
import com.preguicoso.client.busca.ListarItens;
import com.preguicoso.client.estabelecimento.EstabelecimentoUI;
import com.preguicoso.client.estabelecimento.carrinho.Carrinho;
import com.preguicoso.client.htmlEstaticas.QuemSomos;
import com.preguicoso.client.login.Login;
import com.preguicoso.client.telaInicial.CategoriasEstabelecimentos;
import com.preguicoso.client.telaInicial.EscolhaCep;
import com.preguicoso.client.telaInicial.lightBox;
import com.preguicoso.client.user.StatusPedido;

public class RouterHistory {

	public RouterHistory() {
		super();
	}

	public void routerHistory() {
		History.addValueChangeHandler(new ValueChangeHandler<String>() {
			@Override
			public void onValueChange(ValueChangeEvent<String> event) {
				String token = History.getToken();
				if(!token.contains("quem")){
					clearContent();
				}
				buildHomePage(token);
			}

		});
		$("#botao_toggle").live(Event.ONCLICK, new Function() {
			@Override
			public void f() {
				$("#explainConteiner").as(Effects).slideToggle(500);
			}
		});
		History.fireCurrentHistoryState();
		buildHomePage(History.getToken());
	}

	public void buildHomePage(String token) {
//		Login login = Login.getInstance();
//		RootPanel.get("content").setStyleName("container_12");
//		RootPanel.get("login").clear();
//		RootPanel.get("login").add(login.refresh());
//		RootPanel.get("rodape").setVisible(true);
		//EscolhaCep.getInstance().getBairro();

		if (token.startsWith("cep")) {
			Window.Location.replace("../index/index.html");
			RootPanel.get("cadastroEstabelecimento").setVisible(false);
			RootPanel.get("content").setVisible(false);
			RootPanel.get("inicial").setVisible(true);
			RootPanel.get("rodape").setVisible(true);
			
			RootPanel.get("header").setVisible(false);
			
			EscolhaCep cep = EscolhaCep.getInstance();
			cep.setVisible(true);
			RootPanel.get("inicial").clear();
			RootPanel.get("inicial").add(cep);
			
			//$(".middle_rodape").as(Effects).slideDown(1000);
			//clearContent();
			addSuggestBox();
			addListEstabelecimento();
			
		}
		else if (token.startsWith("index")) {
			Window.Location.replace("../index/index.html");
			
			RootPanel.get("cadastroEstabelecimento").clear();
			RootPanel.get("cadastroEstabelecimento").add(new CategoriasEstabelecimentos());
			RootPanel.get("cadastroEstabelecimento").setVisible(true);
			RootPanel.get("header").setVisible(true);
			clearContent();
			RootPanel.get("content").setVisible(true);
			addSuggestBox();
			RootPanel.get("busca").add(new ListarItens());
			addListEstabelecimento();
			//addProdutosEmDestaque();
		} else if (token.equals("quem")) {
			gerarHtml("quem");
		} else if (token.equals("temos")) {
			gerarHtml("temos");
		} else if (token.equals("ajuda")) {
			gerarHtml("ajuda");
		} else if (token.equals("filie")) {
			gerarHtml("filie");
		} else if (token.contains("lista")) {
			ListaEstabelecimento listaEstabelecimento = new ListaEstabelecimento(
					"", token.substring(6));
			RootPanel.get("busca").add(listaEstabelecimento);
		} else if (token.startsWith("Estabelecimento/")) {
			RootPanel.get("content").setStyleName("");
			RootPanel.get("content").setVisible(true);
			RootPanel.get("cadastroEstabelecimento").setVisible(false);
			animateBegin();
			printEstabelicimento(token);
		} else if (token.equals("pedido")) {
			RootPanel.get("cadastroEstabelecimento").setVisible(false);
			animateBegin();
			printStatus(token);
		}
		else if(History.getToken().isEmpty()){
			Window.Location.replace("../index/index.html");
//			History.newItem("cep");	
//			buildHomePage(History.getToken());
		}
	}

	private void addProdutosEmDestaque() {
		RootPanel.get("busca").add(new ListarItens());
		
	}

	private void addListProdutosMaisVendidos() {
		// TODO Auto-generated method stub
		
	}

	public void animateBegin() {
		$("#explainConteiner").as(Effects).slideUp(200);
		$(".middle_rodape").as(Effects).slideUp(200);
		
		// RootPanel.get("cadastroEstabelecimento").clear();
	}

	public void printEstabelicimento(String token) {
		long id;
		String[] nomes = token.split("/");
		if (token.length() > 16) {
			id = Long.parseLong(nomes[1]);
		} else {
			id = 0;
		}
		RootPanel.get("content").clear();
		EstabelecimentoUI page = new EstabelecimentoUI(id);
		RootPanel.get("content").add(page);
	}

	public void printStatus(String token) {
		RootPanel.get("content").add(StatusPedido.getInstance());
	}

	public void clearContent() {
		RootPanel.get("busca").clear();
		RootPanel.get("searchFieldDiv").clear();
		RootPanel.get("content").clear();
		RootPanel.get("inicial").setVisible(false);
		RootPanel.get("header").setVisible(true);
	}

	public void addListEstabelecimento() {
	
		RootPanel.get("busca").add(new ListaEstabelecimento());
	}
	
	public void addSuggestBox() {
		// SUGGEST BOX
		final Busca buscaEstabelecimento = new Busca(Entities.Estabelecimento);
		HTML lupaButton = new HTML(
				"<img style='cursor: pointer;'src='img/lupa.png'/>");
		lupaButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (!buscaEstabelecimento.getSuggestBox().getText().equals("")) {
					RootPanel.get("busca").clear();
					ListaEstabelecimento lista = new ListaEstabelecimento(
						buscaEstabelecimento.getSuggestBox().getText());
					RootPanel.get("busca").add(lista);
				}
			}
		});
		RootPanel.get("searchFieldDiv").add(buscaEstabelecimento);
		lupaButton.getElement().setId("searchFieldImg");
		RootPanel.get("searchFieldDiv").add(lupaButton);
	}

	protected void gerarHtml(String string) {
		if (string.contains("quem")) {
			lightBox.getInstance().show(new QuemSomos(),800,400);
//			String html = "";
//			html += "<h1>Quem Somos</h1>";
//			HTMLPanel quem = new HTMLPanel(html);
//			RootPanel.get("mainConteiner").clear();
//			RootPanel.get("mainConteiner").add(quem);
		}
		if (string.equals("temos")) {
			String html = "";
			html += "<h1>Termos de Compromisso</h1>";
			//HTMLPanel quem = new HTMLPanel(html);
			//RootPanel.get("mainConteiner").clear();
			//RootPanel.get("mainConteiner").add(quem);
		}
		if (string.equals("ajuda")) {
			String html = "";
			html += "<h1>FAC</h1>";
			HTMLPanel quem = new HTMLPanel(html);
			//RootPanel.get("mainConteiner").clear();
			//RootPanel.get("mainConteiner").add(quem);
		}
		if (string.equals("filie")) {
			String html = "";
			html += "<h1>Filie-se</h1>";
			HTMLPanel quem = new HTMLPanel(html);
			//RootPanel.get("mainConteiner").clear();
			//RootPanel.get("mainConteiner").add(quem);
		}

	}

}